package tetris;

import java.util.*;
import java.io.*;

/**
 * Managing the highscore system.
 * @author dejvid
 *
 */
class ScoresSystem {
	private ArrayList<Score> results;
	private static final String PATH = "src/tetris/results/results.dat";
	
	ObjectInputStream input = null;
	ObjectOutputStream output = null;
	
	/**
	 * Create the List which contains data.
	 */
	public ScoresSystem() {
	        results = new ArrayList<Score>();
	}
	
	/**
	 * Responsible for loading and sorting results.
	 * @return returns results
	 */
	public ArrayList<Score> getScores() {
	    loadScores();
	    sortScores();
	    return results;
	}
	
	/**
	 * Sorts results form higher to lower.
	 */
	private void sortScores() {
	    CompareScores compare = new CompareScores();
	    Collections.sort( results, compare );
	}
	
	/**
	 * Adding a result to List which is simple data-base.
	 * @param nickname - player nickname
	 * @param score - player score
	 */
	
	public void addScore( String nickname, int score ) {
	    loadScores();
	    if( score > 0 ) {
	    	results.add( new Score( nickname, score ) );
	        updateScores();
	    }
	    
	}
	
	/**
	 * Loading results from file.
	 */
	@SuppressWarnings("unchecked")
	public void loadScores() {
		try {
	        input = new ObjectInputStream( new FileInputStream( PATH ) );
	        results = (ArrayList<Score>) input.readObject();
	    } 
		catch (FileNotFoundException e) {
	            System.out.println( "File not found" );
	    } 
		catch (IOException e) {
	            System.out.println( e.getMessage() );
	    } 
		catch (ClassNotFoundException e) {
	            System.out.println( e.getMessage() );
	    } 
		finally {
				try {
					if (output != null) {
						output.flush();
	                    output.close();
	                }
	            } 
				catch (IOException e) {
					System.out.println( e.getMessage() );
	            }
		}
	}
	
	/**
	 * Update data file without deleting it.
	 */
	public void updateScores() {
	    try {
	         output = new ObjectOutputStream( new FileOutputStream( PATH ) );
	         output.writeObject( results );
	    } 
	    catch (FileNotFoundException e) {
	    	System.out.println( e.getMessage() );
	    } 
	    catch (IOException e) {
	    	System.out.println( e.getMessage() );
	    } 
	    finally {
		        try {
		        	if ( output != null ) {
		        		output.flush();
		                output.close();
		        	}
		        } 
		        catch (IOException e) {
		        System.out.println( e.getMessage() );
		        }
	    }
	}
	
	/**
	 * Loading on screen top ten results.
	 * @return - return top ten results.
	 */
	public String getTopTen() {
		String highscoreString = "";
	    final int max = 10;
	
	    ArrayList<Score> scores;
	    scores = getScores();
	
	    int i = 0;
	    int x = scores.size();
	    if (x > max) {
	            x = max;
	    }
	    while (i < x) {
	            highscoreString += (i + 1) + ".\t" + scores.get(i).getNickname() + "\t" + scores.get(i).getScore() + "\n";
	                    i++;
	            }
	            return highscoreString;
	        }
	}